# -*- coding : utf-8 -*-

import tqdm
import json
import requests
import threadpool
import pymysql

from openpyxl import load_workbook, Workbook

def faq(file_paths):
    wb_w = Workbook()
    ws_w = wb_w.active
    ws_w.append(['客户ID', '对话记录', '意图标签', '是否拉黑'])

    pool = threadpool.ThreadPool(20)

    for file_path in file_paths:
        wb = load_workbook(file_path)
        ws = wb[wb.sheetnames[0]]

        url = 'http://8.142.85.77:8680/nlu?session_id=-1&workspace=222&current_query={}'

        customer_id_dict = dict()
        for i, row in tqdm.tqdm(enumerate(ws.values)):
            if i != 0 and row[0]:
                customer_id = row[0]
                content = row[1]

                if customer_id not in customer_id_dict:
                    customer_id_dict[customer_id] = list()
                if content.startswith('2_'):
                    customer_id_dict[customer_id].append(content)


        args_list = list()
        for customer_id in tqdm.tqdm(customer_id_dict):
            # faq_task(customer_id, customer_id_dict[customer_id], ws_w)
            args_list.append(([customer_id, customer_id_dict[customer_id], ws_w], None))


        requests = threadpool.makeRequests(faq_task, args_list)
        [pool.putRequest(req) for req in requests]
        pool.wait()

    wb_w.save('拉黑数据5.xlsx')




def faq_task(customer_id, content_list, ws_w):
    url = 'http://8.142.85.77:8680/nlu?session_id=-1&workspace=222&current_query={}'

    intent_list = set()
    sign = False
    for content in content_list:

        response = requests.get(url.format(content))
        response = json.loads(response.text)
        intent = response['standard_query']

        if intent not in ['NOINTENT', '肯定态度', '否定态度', '无态度']:
            intent_list.add(intent)
        if intent in [
            '投诉', '别给我打电话了', '强烈拒绝', '怀疑平台', '询问工号', '你怎么有我号码',
            '无意向调侃用户', '要求挂机', '敏感客群', '怎么注销', '怀疑信息泄露', '人还是机器人', '要求反馈立刻处理', ]:
            # ws_w.append([customer_id, intent])
            sign = True

    intent_list = list(intent_list)
    ws_w.append([customer_id,
                 '\n'.join(content_list), ','.join(intent_list), '拉黑' if sign else ''])



def task(target_list):
    pool = threadpool.ThreadPool(30)
    # executor = ThreadPoolExecutor(max_workers=20)
    file_path = './customer_record_url.txt'
    with open(file_path, 'r') as fp:
        results = fp.readlines() # [0].split(',')
        # print(results)
        results = eval(results[0])
        args_list = list()
        for result in tqdm.tqdm(results):
            # result = eval(result)
            # print(result)
            customer_id = result['customer_id']
            wav_url = result['record_url']

            args_list.append(([customer_id, wav_url, '金条问题5'], None))


    requests = threadpool.makeRequests(platform_task, args_list)
    [pool.putRequest(req) for req in requests]
    pool.wait()


def platform_task(customer_id, voice_path, IP):
    url = 'http://8.142.69.133:9503/quality_test?&customerId={}&wavPath={}&IP={}'.format(
        customer_id, voice_path, IP
    )
    response = requests.get(url)



if __name__ == '__main__':


    faq(['./2024-2-27_金条问题5.xlsx', ])

    # task(customer_id_list)







